﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.TranslateApprove.DAL;
using Yipusoft.TranslateApprove.BLL;
using Yipusoft.Common.Model;
using Yipusoft.Common.BLL;

public partial class Police_Select_SelectAdd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            init();
        }
    }

    void init()
    {
        YearArmy.Text = DateTime.Now.Year.ToString();
        BindJinZhong();
        BindRank();
        if (Request.QueryString["Id"] != null)
        {
            Guid id = new Guid(Request.QueryString["Id"]);
            TranslateSelect info = Select.GetTranslateSelect(id);
            SoldierName.Text = info.SoldierName;
            Sex.SelectedItem.Text = info.Sex;
            ddlRank.SelectedValue = info.Rank;
            YearArmy.Text = info.JoinTime.Value.Year.ToString();
            rblJingZhong.SelectedValue = info.FromJingZhong;
            txtNewDepartment.Text = info.TanslateDepartment;
            OldDepartment.Text = info.OldDepartment;
            OldDepartment2.Text = info.OldDepartment2;
            LinkMan.Text = info.LinkMen;
            Remark.Text = info.Remark;

        }
    }
    private void BindJinZhong()
    {
        rblJingZhong.DataSource = CodeItem.GetList("ParentID='SBDD10' ORDER BY SeqID desc");
        rblJingZhong.DataTextField = "ItemName";
        rblJingZhong.DataValueField = "ItemID";
        rblJingZhong.DataBind();
        rblJingZhong.SelectedIndex = 0;
    }
    private void BindRank()
    {
        ddlRank.Items.Add(new ListItem("", ""));
        foreach (CodeItemInfo item in CodeItem.GetList("ParentID='SBJX00' ORDER BY SeqID"))
        {
            ListItem li = new ListItem(item.ItemName, item.ItemID);
            ddlRank.Items.Add(li);
        }
    }


    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["Id"] != null)
        {
            try
            {
                SelectDataContext db = new SelectDataContext();
                TranslateSelect select = db.TranslateSelect.Where(o => o.Id == new Guid(Request.QueryString["Id"])).FirstOrDefault();
                select.JoinTime = Convert.ToDateTime(YearArmy.Text + "-12-01");
                select.LastDate = DateTime.Now;
                select.LinkMen = LinkMan.Text;
                select.OldDepartment = OldDepartment.Text;
                select.Rank = ddlRank.SelectedValue;
                select.Sex = Sex.Text;
                select.SoldierName = SoldierName.Text;
                select.TanslateDepartment = txtNewDepartment.Text;
                select.OldDepartment2 = OldDepartment2.Text;
                select.TanslateType = 0;
                select.FromJingZhong = rblJingZhong.SelectedValue;
                select.ToJingZhong = "SBDD30";
                select.Remark = Remark.Text;
                db.SubmitChanges();
                Prompt.Text = "修改成功！";
                // Response.Redirect("SelectList.aspx");
            }
            catch (Exception ex)
            {
                Prompt.Text = ex.Message;
            }
        }
        else
        {
            try
            {
                SelectDataContext db = new SelectDataContext();
                TranslateSelect select = new TranslateSelect();
                select.Id = Guid.NewGuid();
                select.JoinTime = Convert.ToDateTime(YearArmy.Text + "-12-01");
                select.LastDate = DateTime.Now;
                select.LinkMen = LinkMan.Text;
                select.OldDepartment = OldDepartment.Text;
                select.Rank = ddlRank.SelectedValue;
                select.Remark = Remark.Text;
                select.Sex = Sex.Text;
                select.SoldierName = SoldierName.Text;
                select.TanslateDepartment = txtNewDepartment.Text;
                select.OldDepartment2 = OldDepartment2.Text;
                select.TanslateType = 0;
                select.FromJingZhong = rblJingZhong.SelectedValue;
                select.ToJingZhong = "SBDD30";
                db.TranslateSelect.InsertOnSubmit(select);
                db.SubmitChanges();
                Prompt.Text = "添加成功！";
                 Response.Redirect("SelectList.aspx");
            }
            catch
            {
                Prompt.Text = "添加失败！";
            }
        }
    }
}
